<?php
namespace app\model\question;

use think\Model;
use app\model\product\ZxlKnowStoreExamContentModel;

class ZxlKnowStoreQuestionPracticeDetailModel extends Model
{
    protected $name = 'zxl_know_store_question_practice_detail';

    /**
     * 获取用户已做题详情
     * 
     * @return array
     */
    public static function get_user_practice_detail($practice_id, $user_id, $question_id)
    {
        $practice_detail = ZxlKnowStoreQuestionPracticeDetailModel::where(['practice_id' => $practice_id, 'question_id' => $question_id, 'user_id' => $user_id])->field('question_id,answer')->find();
        if ($practice_detail) {
            $practice_detail = $practice_detail->toArray();
            $practice_detail['answer'] = json_decode($practice_detail['answer'], true);
            $practice_detail['answer_str'] = implode(',', $practice_detail['answer']);
        }
        return $practice_detail;
    }

    /**
     * 记录答题详情
     * 
     * @return array
     */
    public static function addLog($questionId, $answer, $practice){
        $log = self::where(["user_id" => $practice->user_id, "question_id"=> $questionId, "practice_id" => $practice->id])->findOrEmpty();
        if($log->isEmpty()) {
            $log->user_id = $practice->user_id;
            $log->m_id = $practice->m_id;
            $log->question_id = $questionId;
            $log->lib_id = $practice->lib_id;
            $log->practice_id = $practice->id;
            $log->main_id = $practice->target_id;
            $log->create_time = time();
            // 查询题目对应的chapterID
            if($practice->practice_type == "chapter"){ // 章节练习
                $log->main_id = ZxlKnowStoreQuestionModel::where("id", $questionId)->value("chapter_id");
            }
        }
        
        $log->update_time = time();
        $log->answer = json_encode($answer, JSON_UNESCAPED_UNICODE);
        $log->is_correct = ZxlKnowStoreQuestionModel::isCorrect($questionId, $log->answer);
        if($practice->practice_type != "wrong" && $practice->practice_type != "fallibility") {
            $wrongLog = ZxlKnowStoreQuestionPracticeWrongModel::where(["practice_id" => $practice->id, "question_id"=> $questionId, 'user_id' => $practice->user_id])->findOrEmpty();
            if($log->is_correct == 0 && $wrongLog->isEmpty()){
                $wrongLog->user_id = $practice->user_id;
                $wrongLog->m_id = $practice->m_id;
                $wrongLog->question_id = $questionId;
                $wrongLog->lib_id = $practice->lib_id; 
                $wrongLog->answer = $log->answer;
                $wrongLog->practice_id = $practice->id;
                $wrongLog->practice_type = $practice->practice_type;
                $wrongLog->question_type = ZxlKnowStoreQuestionModel::where("id", $questionId)->value("type");
                $wrongLog->chapter_id =  ZxlKnowStoreQuestionModel::where("id", $questionId)->value("chapter_id") ?? 0;
                $wrongLog->is_practice = 0;
                $wrongLog->save();
            }
            if($log->is_correct == 1 && !$wrongLog->isEmpty()) {
                $wrongLog->delete();
            }
        } 
        
        // 考试
        if($practice->practice_type == "professional") {
            $score = ZxlKnowStoreExamContentModel::where(["product_id" => $practice->target_id, "question_id" => $questionId])
            ->value("score") ?? 0;
            if($score > 0 && $log->is_correct == 1) {
                $log->score = $score;
            }          
        }
        $log->save();
        if($practice->practice_type == "chapter") { 
            $chapterId = ZxlKnowStoreQuestionModel::whereIn("id", $questionId)->value("chapter_id") ?? 0;
            if($chapterId > 0){
                $chapter = ZxlKnowStoreQuestionChapterModel::where("id", $chapterId)->find();
                if($chapter) ZxlKnowStoreQuestionUserStatisticsModel::UpdateUserData($chapter, $practice->user_id, $practice->m_id, $practice->lib_id);
            }
        }
        return $log;
    }
}